<?php

include("mysql_conn.php");

$username=$_GET["username"];
$mark=$_GET["mark"];
$now=time();
$datestamp=date("Ymd", $now);
$tmpfilename="status-$username-$datestamp.html";
$path="/u02/webapps/baugust/itg-status/status_reports/";

$tmpfile=fopen("../status_reports/$tmpfilename","w+");
// $pdffile=fopen("../status_reports/$username/$filename","w+");

//create an output buffer
function write_to_file($buffer)
{
  global $tmpfile;
  fwrite($tmpfile,$buffer);
}

//start it up
ob_start('write_to_file');

//do stuff
include("userreport.php");

//end it
ob_end_flush();

//convert to string

$htmlout=file_get_contents("../status_reports/$tmpfilename");

// prep the data

$user=mysql_real_escape_string($username);
$data=mysql_real_escape_string($htmlout);

//insert into mysql

$sql="insert into status_reports (user, report_data)
values
('$user', '$data')";

if (!mysql_query($sql,$con))
  {
  die('Error: ' . mysql_error());
  }

//then we delete the temp file
unlink("../status_reports/$tmpfilename");

// now if you asked for it, we will mark the reported current updates as old.
if($mark=='1')

{
  $sql="update updates u, projects p set u.isold='1' where p.projectid=u.projectid and p.username='$username' and u.updatetype='T' and u.isold='0'";   

  if (!mysql_query($sql,$con))
  {
   die('Error: ' . mysql_error());
  }
 
 $sql2="update projects p set state='2' where username='$username' and state='1'";

  if (!mysql_query($sql2,$con))
  {
   die('Error: ' . mysql_error());
  }

}

echo "<html><head><meta http-equiv='refresh' content='0;url=myprojects.php'><link rel='stylesheet' type='text/css' href='style.css'></head><body> Status report saved successfully.<p> <a href='myprojects.php'>Return to My Projects</a> </body></html>";

?>
